<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3
 * Time: 21:54
 */

namespace App\Game\Traits;


use App\Models\Def\MainGamePlat;
use App\Models\Inf\Player;
use Illuminate\Support\Facades\Cache;

trait ScTrait
{
    public function userToken($username, $password = null, $site_username)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_name', $this->platName)->first();
        $player = Player::where([
                ['main_game_plats_id', '=', $mainGamePlat->id],
                ['username', '=', $username],
            ]
        )->first();
        $password = $password ?? $player->password;
        $params = [
            'plat' => 'sc',
            'site_username' => $site_username,
            'username' => $username,
            'password' => $password,
            'ip' => request()->getClientIp()
        ];
        $token = encrypt(json_encode($params));
        Cache::put($username, $token, 3);

        return $token;
    }

    public function rechargeToken($username, $site_username, $price, $op)
    {
        $params = [
            'plat' => 'sc',
            'site_username' => $site_username,
            'username' => $username,
            'price' => $price,
            'op' => $op
        ];
        $token = encrypt(json_encode($params));
        Cache::put($username . '_transfer', $token, 3);

        return $token;
    }

    public function scRequest(array $params, $url)
    {
        try{
            $url = $this->gameUrl . $url;
            $params = array_merge(['roomMasterId' => $this->roomMasterId], $params);
            $hash = md5(json_encode($params) . $this->appKey);
            $url = $url . '?hash=' . $hash;
            $header = [
                "Accept" => "application/json",
                "Content-Type" => "application/json",
            ];
            $result = $this->request('POST', $url, [], $header, ['json' => $params]);
            return json_decode($result, true);
        }catch (\Exception $e){
            $err = [
                'code' => $e->getCode(),
                'msg'  => $e->getMessage(),
                'file'    => $e->getFile(),
                'line'   => $e->getLine()
            ];
            file_put_contents('/home/gameapi/storage/logs/test.log', 'sc_error...'.json_encode($err).PHP_EOL, FILE_APPEND);
        }
    }

    /**
     * gameapi
     * 2018/9/24 17:35
     * Administrator
     * apiDataParse
     * 字段解析
     * @param array $apiData
     * @param array $data
     * @return array
     */
    public function apiDataParse(array $apiData, array $data)
    {
        $data = $this->statusParse($apiData, $data);
        return $data;
    }

    /**
     * gameapi
     * 2018/9/24 17:36
     * Administrator
     * statusParse
     * 解析状态字段
     * @param array $apiData
     * @param array $data
     * @return array
     */
    private function statusParse(array $apiData, array $data)
    {
        if (array_key_exists('is_getprize', $apiData)) {
            switch ($apiData['is_getprize']) {
                case '0':
                    $data['status'] = 0;
                    break;
                case '1':
                    $data['status'] = 1;
                    break;
                case '2':
                    $data['status'] = 1;
                    break;
                default:
                    $data['status'] = 1;
                    break;
            }
        }

        if (array_key_exists('is_cancel', $apiData)) {
            if ($apiData['is_cancel'] != 0) {
                $data['status'] = -1;
            }
        }
        return $data;
    }
}